@file:Suppress("SpellCheckingInspection")

package han.cirno.cirno_color

import org.gradle.api.Plugin
import org.gradle.api.Project
import java.io.File
import java.io.FileOutputStream
import java.io.PrintWriter

@Suppress("unused")
class ColorPlugin: Plugin<Project> {
    companion object{
        const val TASK_NAME="cirnoColor"
        const val COLORS="""
#FFCDD2:red_100
#EF9A9A:red_200
#E57373:red_300
#EF5350:red_400
#F44336:red_500
#E53935:red_600
#D32F2F:red_700
#C62828:red_800
#B71C1C:red_900
#FF8A80:red_A100
#FF5252:red_A200
#FF1744:red_A400
#D50000:red_A700
#F8BBD0:pink_100
#F48FB1:pink_200
#F06292:pink_300
#EC407A:pink_400
#E91E63:pink_500
#D81B60:pink_600
#C2185B:pink_700
#AD1457:pink_800
#880E4F:pink_900
#FF80AB:pink_A100
#FF4081:pink_A200
#F50057:pink_A400
#C51162:pink_A700
#E1BEE7:purple_100
#CE93D8:purple_200
#BA68C8:purple_300
#AB47BC:purple_400
#9C27B0:purple_500
#8E24AA:purple_600
#7B1FA2:purple_700
#6A1B9A:purple_800
#4A148C:purple_900
#EA80FC:purple_A100
#E040FB:purple_A200
#D500F9:purple_A400
#AA00FF:purple_A700
#D1C4E9:dpurple_100
#B39DDB:dpurple_200
#9575CD:dpurple_300
#7E57C2:dpurple_400
#673AB7:dpurple_500
#5E35B1:dpurple_600
#512DA8:dpurple_700
#4527A0:dpurple_800
#311B92:dpurple_900
#B388FF:dpurple_A100
#7C4DFF:dpurple_A200
#651FFF:dpurple_A400
#6200EA:dpurple_A700
#C5CAE9:indigo_100
#9FA8DA:indigo_200
#7986CB:indigo_300
#5C6BC0:indigo_400
#3F51B5:indigo_500
#3949AB:indigo_600
#303F9F:indigo_700
#283593:indigo_800
#1A237E:indigo_900
#8C9EFF:indigo_A100
#536DFE:indigo_A200
#3D5AFE:indigo_A400
#304FFE:indigo_A700
#BBDEFB:blue_100
#90CAF9:blue_200
#64B5F6:blue_300
#42A5F5:blue_400
#2196F3:blue_500
#1E88E5:blue_600
#1976D2:blue_700
#1565C0:blue_800
#0D47A1:blue_900
#82B1FF:blue_A100
#448AFF:blue_A200
#2979FF:blue_A400
#2962FF:blue_A700
#B3E5FC:lblue_100
#81D4FA:lblue_200
#4FC3F7:lblue_300
#29B6F6:lblue_400
#03A9F4:lblue_500
#039BE5:lblue_600
#0288D1:lblue_700
#0277BD:lblue_800
#01579B:lblue_900
#80D8FF:lblue_A100
#40C4FF:lblue_A200
#00B0FF:lblue_A400
#0091EA:lblue_A700
#B2EBF2:cyan_100
#80DEEA:cyan_200
#4DD0E1:cyan_300
#26C6DA:cyan_400
#00BCD4:cyan_500
#00ACC1:cyan_600
#0097A7:cyan_700
#00838F:cyan_800
#006064:cyan_900
#84FFFF:cyan_A100
#18FFFF:cyan_A200
#00E5FF:cyan_A400
#00B8D4:cyan_A700
#B2DFDB:teal_100
#80CBC4:teal_200
#4DB6AC:teal_300
#26A69A:teal_400
#009688:teal_500
#00897B:teal_600
#00796B:teal_700
#00695C:teal_800
#004D40:teal_900
#A7FFEB:teal_A100
#64FFDA:teal_A200
#1DE9B6:teal_A400
#00BFA5:teal_A700
#C8E6C9:green_100
#A5D6A7:green_200
#81C784:green_300
#66BB6A:green_400
#4CAF50:green_500
#43A047:green_600
#388E3C:green_700
#2E7D32:green_800
#1B5E20:green_900
#B9F6CA:green_A100
#69F0AE:green_A200
#00E676:green_A400
#00C853:green_A700
#DCEDC8:lgreen_100
#C5E1A5:lgreen_200
#AED581:lgreen_300
#9CCC65:lgreen_400
#8BC34A:lgreen_500
#7CB342:lgreen_600
#689F38:lgreen_700
#558B2F:lgreen_800
#33691E:lgreen_900
#CCFF90:lgreen_A100
#B2FF59:lgreen_A200
#76FF03:lgreen_A400
#64DD17:lgreen_A700
#F0F4C3:lime_100
#E6EE9C:lime_200
#DCE775:lime_300
#D4E157:lime_400
#CDDC39:lime_500
#C0CA33:lime_600
#AFB42B:lime_700
#9E9D24:lime_800
#827717:lime_900
#F4FF81:lime_A100
#EEFF41:lime_A200
#C6FF00:lime_A400
#AEEA00:lime_A700
#FFF9C4:yellow_100
#FFF59D:yellow_200
#FFF176:yellow_300
#FFEE58:yellow_400
#FFEB3B:yellow_500
#FDD835:yellow_600
#FBC02D:yellow_700
#F9A825:yellow_800
#F57F17:yellow_900
#FFFF8D:yellow_A100
#FFFF00:yellow_A200
#FFEA00:yellow_A400
#FFD600:yellow_A700
#FFECB3:amber_100
#FFE082:amber_200
#FFD54F:amber_300
#FFCA28:amber_400
#FFC107:amber_500
#FFB300:amber_600
#FFA000:amber_700
#FF8F00:amber_800
#FF6F00:amber_900
#FFE57F:amber_A100
#FFD740:amber_A200
#FFC400:amber_A400
#FFAB00:amber_A700
#FFE0B2:orange_100
#FFCC80:orange_200
#FFB74D:orange_300
#FFA726:orange_400
#FF9800:orange_500
#FB8C00:orange_600
#F57C00:orange_700
#EF6C00:orange_800
#E65100:orange_900
#FFD180:orange_A100
#FFAB40:orange_A200
#FF9100:orange_A400
#FF6D00:orange_A700
#FFCCBC:dorange_100
#FFAB91:dorange_200
#FF8A65:dorange_300
#FF7043:dorange_400
#FF5722:dorange_500
#F4511E:dorange_600
#E64A19:dorange_700
#D84315:dorange_800
#BF360C:dorange_900
#FF9E80:dorange_A100
#FF6E40:dorange_A200
#FF3D00:dorange_A400
#DD2C00:dorange_A700
#D7CCC8:brown_100
#BCAAA4:brown_200
#A1887F:brown_300
#8D6E63:brown_400
#795548:brown_500
#6D4C41:brown_600
#5D4037:brown_700
#4E342E:brown_800
#3E2723:brown_900
#F5F5F5:grey_100
#EEEEEE:grey_200
#E0E0E0:grey_300
#BDBDBD:grey_400
#9E9E9E:grey_500
#757575:grey_600
#616161:grey_700
#424242:grey_800
#212121:grey_900
#CFD8DC:bgrey_100
#B0BEC5:bgrey_200
#90A4AE:bgrey_300
#78909C:bgrey_400
#607D8B:bgrey_500
#546E7A:bgrey_600
#455A64:bgrey_700
#37474F:bgrey_800
#263238:bgrey_900
"""
    }

    override fun apply(project: Project) {
        project.tasks.register(TASK_NAME) {t->
            t.doLast {_->
                val rootDir = project.layout.buildDirectory.dir("cirno-color").get().asFile
                val resDir=File(rootDir,"res").also { it.mkdirs() }
                val javaDir=File(rootDir,"java").also { it.mkdirs() }
                genRes(resDir)
                genJava(javaDir)
            }
        }
        project.afterEvaluate {
            project.tasks.find { it.name=="preBuild" }?.dependsOn(TASK_NAME)
        }
    }

    private fun genRes(resDir:File){
        val valuePath = File(resDir, "values").also { if (!it.exists()) it.mkdirs() }
        val colorsXml = File(valuePath, "colors_gen.xml").also { if (!it.exists())it.createNewFile() }
        FileOutputStream(colorsXml).use {o->PrintWriter(o).use { w->
            w.println("<?xml version=\"1.0\" encoding=\"utf-8\"?>")
            w.println("<!-- Material Design Color Card - Generated by cirno-color -->")
            w.println("<resources>")
            w.println(COLORS.lines().joinToString ("\n") {
                if(it.isEmpty())return@joinToString ""
                val strings = it.split(":")
                "    <color name=\"${strings[1]}\">${strings[0]}</color>"
            })
            w.println("</resources>")
        }}
    }

    private fun genJava(javaDir:File){
        val packagePath=File(javaDir,"han/cirno/cirno_color").also { if (!it.exists())it.mkdirs() }
        val javaFile=File(packagePath,"ColorSelector.kt").also { if(!it.exists())it.createNewFile() }
        val list= arrayOf(
            "package han.cirno.cirno_color",
            "import han.cirno.corrupt.R",
            "import javax.annotation.processing.Generated",
            "/**",
            " * Generated by cirno_color",
            " */",
            "@Generated",
            "object ColorSelector{",
            "    val colors:Array<Pair<String,Int>> =arrayOf(",
            COLORS.lines().mapNotNull {
                if(it.isEmpty())return@mapNotNull null
                val strings=it.split(":")
                "        Pair(\"${strings[1]}\",0x${strings[0].substring(1)}),"
            }.joinToString("\n"),
            "    )",
            "}"
        )
        FileOutputStream(javaFile).use { o->PrintWriter(o).use { w->w.println(list.joinToString("\n")) } }
    }
}